//
//  AppDelegate.m
//  V-Learn
//
//  Created by Apple on 2018/12/19.
//  Copyright © 2018 yxk. All rights reserved.
//

#import "AppDelegate.h"
#import "LBNavigationController.h"
#import "LBTabBarViewController.h"
#import "LBLoginVC.h"
#import <UMCommon/UMCommon.h>
#import <UMShare/UMShare.h>
#import "WXAuth.h"
#import "AppDelegate+WechatPayService.h"
/**微信*/  //万科  wxa365b89a1c54ed49  需要重置一下appscret 才可以使用
#define WeChatKey @"wxa365b89a1c54ed49"
#define WeChatAppSecret @"67da161504d4cad15a2e82fc3b8f0ce1"
/**朋友圈*/
#define FriendKey @"wxa365b89a1c54ed49"
#define FriendAppSecret @"67da161504d4cad15a2e82fc3b8f0ce1"
#import "WXApi.h"
//#import <AlipaySDK/AlipaySDK.h>
@interface AppDelegate ()<WXApiDelegate,WXApiLogDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor clearColor];
    
    LBLoginVC *vc = [[LBLoginVC alloc] init];
//    LBTabBarViewController *tab = [[LBTabBarViewController alloc] init];
    LBTabBarViewController *tab = [LBTabBarViewController shareInstance];
//    LBNavigationController *navi = [[LBNavigationController alloc] initWithRootViewController:vc];
    

    [self setupThiredPlatform];
    self.window.rootViewController = tab;
    
    //
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)setupThiredPlatform{
    
    //向微信注册
//    [WXApi registerApp:WeChatKey];
    [self hql_configureForWechatPay];
    
    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:YES];
    //友盟分享  使用微信自带的 分享
//    [UMConfigure initWithAppkey:@"" channel:@"App Store"];
    
    //微信
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WeChatKey appSecret:WeChatAppSecret redirectURL:@"http://mobile.umeng.com/social"];

    //微信朋友圈
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatTimeLine appKey:FriendKey appSecret:FriendAppSecret redirectURL:@"http://mobile.umeng.com/social"];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    
    return [WXAUTH handleOpenURL:url];
}


- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
////            NSLog(@"result = %@",resultDic);
//        }];
        
        // 授权跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
////            NSLog(@"result = %@",resultDic);
//            // 解析 auth code
//            NSString *result = resultDic[@"result"];
//            NSString *authCode = nil;
//            if (result.length>0) {
//                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
//                for (NSString *subResult in resultArr) {
//                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
//                        authCode = [subResult substringFromIndex:10];
//                        break;
//                    }
//                }
//            }
////            NSLog(@"授权结果 authCode = %@", authCode?:@"");
//        }];
    }else if([url.host isEqualToString:@"pay"]){
        //微信
        return [WXApi handleOpenURL:url delegate:self];
        
    }else{
        return [WXAUTH handleOpenURL:url];
    }
    return YES;
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
////            NSLog(@"result = %@",resultDic);
//        }];
        
        // 授权跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
////            NSLog(@"result = %@",resultDic);
//            // 解析 auth code
//            NSString *result = resultDic[@"result"];
//            NSString *authCode = nil;
//            if (result.length>0) {
//                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
//                for (NSString *subResult in resultArr) {
//                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
//                        authCode = [subResult substringFromIndex:10];
//                        break;
//                    }
//                }
//            }
////            NSLog(@"授权结果 authCode = %@", authCode?:@"");
//        }];
    }else if([url.host isEqualToString:@"pay"]){
        
            return  [WXApi handleOpenURL:url delegate:self];
    
    }else{
        return [WXAUTH handleOpenURL:url];
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
